#include <windows.h>
//#include <mmsystem.h> //与多媒体有关的大多数接口。
//#pragma comment(lib,"winmm.lib") // Windows多媒体相关应用程序接口,用于低档的音频和游戏手柄。PlaySound()
//Windows项目要使用Windows子系统, 而不是Console, 可以这样设置:
//[Project] --> [Settings] --> 选择"Link"属性页,
//在Project Options中将/subsystem:console改成/subsystem:windows
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // 回调函数(消息处理函数)声明
// 提供默认的消息处理功能(响应各种因用户输入而引发的消息),将消息回传给Windows。
// Windows调用此函数(Windows给程序发消息)
// 主函数
// WINAPI是一个Windows定义的宏,将使系统以特定于Windows API函数的某种特殊方式处理函数名和实参
int WINAPI WinMain(
HINSTANCE hInstance, // 程序可启动多个副本,该参数是指向某个实例的句柄
HINSTANCE hPrevInstance, // 16位Windows操作系统的继承,在当前版本中,该参数始终为空即可
PSTR szCmdLine, // 指向启动程序的命令行字符串的指针
int iCmdShow) // 指定窗口显示方式(正常、最小化)
{
//1 Define our window's attributes(告诉Windows该程序需要的窗口种类)
//WNDCLASS wc; // Structure to hold our window's attributes
WNDCLASSEX wc;
// WNDCLASSEX相对于WNDCLASS增加了两个成员,cbSize 和 hIconSm,必须设置;
// 注册函数名是RegisterClassEx,而不是RegisterClass
static TCHAR szAppName[] = TEXT("Hello Win32"); // Define window name
wc.cbSize = sizeof(WNDCLASSEX); // Set structure size
wc.style = CS_HREDRAW | CS_VREDRAW; // 位运算组合常量,产生一个复合值来指定窗体风格, redraw the window if the size changes
wc.lpfnWndProc = WndProc; // Define the message handling function
wc.cbClsExtra = 0; // No extra bytes after the window class
wc.cbWndExtra = 0; // Structrue or the window instance
//如当需要关联其他数据与窗口的每个实例,以参与各个窗口实例的消息处理过程时,需要额外空间
wc.hInstance = hInstance; // Application instance handle当前实例句柄
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Set default application icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Set window cursor to be the standard arrow
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Set gray brush for background color
wc.lpszMenuName = NULL; // No menu
wc.lpszClassName = szAppName; // Set class name
wc.hIconSm = NULL; // Default small icon
// CS_HREDRAW是指如果窗口的水平宽度改变,则重画该窗口,或运算是指水平、垂直宽度改变时,都重画
// 窗口样式选项通过将32位字中独特的某个位设置为1来定义,可以使用按位或运算来组合。这些表示某种特定样式的位通常称为标志。
//2 Now we register our window,Windows提取并记录所有结构成员的设定值
RegisterClassEx(&wc);
//3 Now we can create the window
// 在Windows知道我们需要的窗口的一般特性以及为该窗口处理消息的函数是什么时,就可以创建该窗口
HWND hwnd; // Window handle, 整数,许多API用其作为参数
hwnd = CreateWindow( // 创建窗口的函数的实参将添加一些附加(个性)的特性,因为应用程序可以有多个窗口
szAppName, // Window class name ,标题栏文本
TEXT("A Window API Program Test"), // Window title
WS_OVERLAPPEDWINDOW, // Window style as overlapped
// 接下来4个参数确定窗口在屏幕上的位置(前两个参数表示坐标)和大小(后两个表示宽、高)
CW_USEDEFAULT, // Initial x position, upper left,左上角坐标,x是从左往右,y是从上往下
CW_USEDEFAULT, // Initial y position, corner of our window as x,y...
CW_USEDEFAULT, // Initial x size, default window size
CW_USEDEFAULT, // Initial y size, ...
NULL, // Parent window handle,NULL表示不是子窗口,否则要指定父窗口的句柄
NULL, // Window menu handle,NULL表示不需要菜单
hInstance, // Program instance handle,当前程序实例句柄
NULL); // Creation parameters,表示简单窗口,如是MDI,要指向某个与此相关的结构
// CreateWindowEx()可以用来以扩展的样式信息创建窗口
//4 Display the window
ShowWindow(hwnd, iCmdShow); // 参数说明要显示的窗口的句柄以及显示窗口的方式
//5 Redraw window client area
UpdateWindow(hwnd); // 初始化程序窗口(重画工作区,也就是在工作区中输出信息)
//6 The message loop(检索属于该程序的Windows消息)
MSG msg; // windows message structure
while (GetMessage( // Get any messages,抢先式多任务机制(旧版Windows是协作式多任务机制)
// 当检索到WM_QUIT消息时结束应用程序,消息循环终止
&msg,
NULL, // NULL表示检索应用程序的所有消息,可用来单独地为某一个窗口检索消息
0, 0)) // 希望从队列中检索消息ID的最小值和最大值,设置为0表示检索所有消息。实现有选择性地检索消息。可以只检索鼠标或只检索键盘消息
{
TranslateMessage(&msg); // Translate the message,为与键盘有关的消息做一些转换工作
DispatchMessage(&msg); // Dispatch the message,调用回调函数WndProc()
}
return msg.wParam; // End, so return to windows
}
//7 用于消息处理的回调函数的定义,由Windows调用
// CALLBACK与前面的WINAPI一样,用来说明函数实参的处理方式
LRESULT CALLBACK WndProc( // Windows调用,所以你有此文件看不到调用代码
HWND hwnd, // 一个指向致使该消息发生的事件所在的窗口
UINT message, // 消息ID,指出消息类型的32位整数值
WPARAM wParam, // 包含与消息各类有关的其他信息,是32位或64位的值
LPARAM lParam) // 包含与消息各类有关的其他信息,是32位或64位的值
{
HDC hdc; // Display context handle
PAINTSTRUCT ps; // structure defining area to be drawn
RECT rect; // A working rectangle
switch (message) // Precess selected messages
{
case WM_CREATE:
//PlaySound(TEXT("hellowin32.wav"), NULL, SND_FILENAME | SND_ASYNC);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps); // Prepare to draw the window
GetClientRect(hwnd, &rect); // Get upper left and lower right of client area
SetBkMode(hdc,TRANSPARENT); // Set text background mode
DrawText( // Now draw the text in the window client area
hdc, // Device context handle
TEXT("Windows API程序:窗口定义、注册、创建、显示、重画、消息循环,消息处理"), // _T(),
-1, // Indicate null terminated string
&rect, // rectangle in which text is to be drawn
DT_SINGLELINE | // Text format: single line
DT_CENTER | // Text format: centered in the line
DT_VCENTER); // Text format: line centered in rect
EndPaint(hwnd, &ps); // Terminate window redraw operation
return 0;
case WM_DESTROY: // Window is being destroyed
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
/*
struct MSG{
HWND hwnd; // Handle for the relevant window
UNIT message; // The message ID
WPARAM wParam; // Message parameter( 32-bits)
LPARAM lParam; // Message parameter( 32-bits)
DWORD time; // The time when the message was queued
POINT pt; // The mouse position
};
*/
本页共132段,7071个字符,9336 Byte(字节)